package com.ylink.yfa4j.server.rpc;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.web.client.RestTemplate;

import com.framework.bdf4j.comm.SpringContextHolder;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author lmc
 * @date 2020/04/30
 */
@Slf4j
public class RpcService {

	@SuppressWarnings("unchecked")
	public Map<String, Object> invokeRpc(String serviceName, String appName, String busCode,
			Map<String, Object> busdata) {
		log.error("invokeRpc 方法在单体应用不支持服务名调用，检查组件使用是否正确！");
		return null;
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> invokeRpcUrl(String serviceUrl, String appName, String busCode,
			Map<String, Object> busdata) {
		Map<String, Object> mapResp = null;
		try {
			RestTemplate restTemplate = SpringContextHolder.getBean("singleTemplate");
			String urlhead = "";
			if (serviceUrl.startsWith("http://")) {
				urlhead = serviceUrl;
			} else {
				urlhead = "http://" + serviceUrl;
			}
			String strUrl = urlhead + "/doBusiness/" + appName + "/" + busCode;
			mapResp = restTemplate.postForObject(strUrl, busdata == null ? new HashMap<String, Object>() : busdata,
					Map.class);
		} catch (Exception e) {
			log.error("RSXX0007:" + e.getMessage(), e);
		}
		return mapResp;
	}

	/**
	 * <b>方法描述：</b> 调用第三方Rest接口 <br/>
	 * <b>创建者：</b> 喻洪乾 <br/>
	 * <b>创建时间：</b> 2019年4月17日 上午11:58:31 <br/>
	 *
	 * @param url inPara 输入参数
	 * @return Rest接口返回
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Object invokeOuterBusiness(String strUrl, Object reqPara) throws Exception {
		log.error("invokeOuterBusiness 方法在单体应用不支持服务名调用，检查组件使用是否正确！");
		return null;
	}

	public Object getBean(String name) {
		String[] bs= SpringContextHolder.getApplicationContext().getBeanDefinitionNames();
		for (String b : bs) {
//			log.info(b);
			if(name.equalsIgnoreCase(b))
				name=b;
		}
		log.info("name==={}",name);
		return SpringContextHolder.getBean(name);
	}

	public void setDynamicDataSourceContext(String name) {
	}

	public DataSource getDataSource(String db) {
		return SpringContextHolder.getBean(DataSource.class);
	}

}
